package com.jic.manager.controller.manage.conf;


import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.conf.api.manage.SysConfigApi;
import com.jic.manager.entity.SysConfig;
import com.jic.conf.request.SysConfigRequest;
import com.jic.conf.response.SysConfigResponse;
import com.jic.manager.service.SysConfigService;
import com.jic.manager.utils.BeanUtil;
import com.jic.manager.entity.SysConfig;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("根据key查询单个字典表")
@RestController
@RequestMapping("/sysConfigByKey")
@Slf4j
public class SysConfigbyKeyController implements SysConfigApi {

    @Autowired
    private SysConfigService sysConfigService;


    /**
     * 根据key查询单个字典表
     * @author zxl
     * @param sysConfigRequest
     * @return
     */
    @ApiOperation(value = "根据key查询单个字典表")
    @ResponseBody
    @RequestMapping("/getInfoByKey")
    @Override
    public RestResult<List<SysConfigResponse>> getInfoByKey(@RequestBody SysConfigRequest sysConfigRequest){
        RestResult restResult = new RestResult();
        try {
            if (sysConfigRequest==null){
                throw new Exception("必传参数为空 key");
            }
            SysConfig sysconfig =  BeanUtil.copy(sysConfigRequest,SysConfig.class);
            sysconfig.setKey(sysConfigRequest.getKeys());
            sysconfig.setIds(sysConfigRequest.getIds());
            List<SysConfig> sysConfig= sysConfigService.query(sysconfig);
            List<SysConfigResponse> sysConfigResponses = JSON.parseArray(JSON.toJSONString(sysConfig), SysConfigResponse.class);
            restResult=RestResult.success(sysConfigResponses);

        }catch (Exception e){
            log.error("查询字典项异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }






}
